import { createRouter, createWebHistory } from "vue-router";
import { zlDialog } from "../utils/zlDialog"
import { openBindAreaDialog } from "../utils/bindAreaDialog"
import { sleep } from "../utils/sleep"
import jsCookie from "js-cookie"
import { useUserStore } from "../store/user"

// 导出pc路由
export const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/home",
    name: "home",
    meta: {
      checkToken: false
    },
    component: () => import("../views/home/index.vue")
  },
  {
    path: "/detail",
    name: "detail",
    meta: {
      checkToken: false
    },
    component: () => import("../views/detail/index.vue")
  },
  {
    path: "/privacy",
    name: "privacy",
    meta: {
      checkToken: false
    },
    component: () => import("../views/privacy/index.vue")
  },
  {
    path: "/agreement",
    name: "agreement",
    meta: {
      checkToken: false
    },
    component: () => import("../views/agreement/index.vue")
  },
  {
    path: "/aboutUs",
    name: "aboutUs",
    meta: {
      checkToken: false
    },
    component: () => import("../views/aboutUs/index.vue")
  },
  {
    path: "/personalCenter",
    name: "personalCenter",
    meta: {
      checkToken: true
    },
    component: () => import("../views/personalCenter/index.vue")
  },
  {
    path: "/vipCenter",
    name: "vipCenter",
    meta: {
      checkToken: true
    },
    component: () => import("../views/vipCenter/index.vue")
  },
  {
    path: "/matching",
    name: "matching",
    meta: {
      checkToken: true
    },
    component: () => import("../views/matching/index.vue")
  },
  {
    path: "/matchingRes",
    name: "matchingRes",
    meta: {
      checkToken: true
    },
    component: () => import("../views/matching/view/matchingRes.vue")
  },
  {
    path: "/relevancePolicy",
    name: "relevancePolicy",
    meta: {
      checkToken: true
    },
    component: () => import("../views/relevancePolicy/index.vue")
  },
  {
    path: "/analysis",
    name: "analysis",
    meta: {
      checkToken: true
    },
    component: () => import("../views/analysis/index.vue")
  },
  {
    path: "/relationImg",
    name: "relationImg",
    meta: {
      checkToken: true
    },
    component: () => import("../views/relationImg/index.vue")
  },{
    path: "/protocol",
    name: "protocol",
    meta: {
      checkToken: true
    },
    component: () => import("../views/protocol/index.vue")
  },{
    path: "/test",
    name: "test",
    meta: {
      checkToken: true
    },
    component: () => import("../views/matching/test.vue")
  },{
    path: "/qAndA/index",
    name: "qAndA",
    meta: {
      checkToken: true
    },
    component: () => import("../views/qAndA/index.vue")
  },{
    path: "/declare/index",
    name: "declare",
    meta: {
      checkToken: true
    },
    component: () => import("../views/declare/index.vue")
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由拦截守卫
router.beforeEach(async function (to, from, next) {
  const useStore = useUserStore()

  // 没有token
  if (!jsCookie.get("token")) {
    // 当前页面需要token
    if (to.meta.checkToken) {
      next(from.path)
      return zlDialog()
    }
  }

  // wait userinfo data
  await useStore.getUserInfo()


  // have a token but not bind address
  // console.log(jsCookie.get("token"));

  const address: any = jsCookie.get("address") == undefined ? "[]" : jsCookie.get("address")
  if (!JSON.parse(address)?.length && jsCookie.get("token") && jsCookie.get("userID") as any != 150) {
    // useStore.updateUserAuth()
    openBindAreaDialog()
  }
  next()
})

export default router;
